package screen.component
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Quart;
	import com.greensock.plugins.BezierThroughPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import helper.ValueObject;

	public class CamiAirplane extends Airplance
	{
		private var _no:Number;//1,2,3
		private const _DISTANCE:Number = 50;
		private const _DURATION:Number =2;
		private const _Y_ODINAL:Number = 70;
		public function CamiAirplane(no:Number):void
		{
			_no = no;
			init();
		}
		public function init():void{
			TweenPlugin.activate([BezierThroughPlugin]); //activation is permanent in the SWF, so this line only needs to be run once.
			_airplane = ValueObject.C1_RESOURCE.getExportedAsset("airplaneCami"+_no.toString()) as MovieClip;
			var distance:Number = ValueObject.WIDTH/4;
			_airplane.x = (_no-3)*distance;
			_airplane.y = _Y_ODINAL;
			addChild(_airplane);
			TweenLite.to(_airplane,_DURATION,{x:_no*distance});
			_airplane.buttonMode = true;
		}
		public function checkShoot(disp:DisplayObject):Boolean{
			if(_airplane.hitTestObject(disp)){
				//airplaneDown();
				_airplane.gotoAndPlay("openPU");
				setTimeout(airplaneDownComplete,1000);
				return true;
			}
			return false;
			//Tank.getInstance().shootCamiAirPlane(this);
		}
		private  function airplaneDown():void{
			var arr:Array = generalOdinalDown();
			TweenLite.to(_airplane,_DURATION,{bezierThrough:arr,orientToBezier:true,rotation:180,onComplete:airplaneDownComplete,ease:Quart.easeIn});
		}
		private function generalOdinalDown():Array{
			var arr:Array =[];
			var y_distance:Number  = (ValueObject.HEIGHT/2-_airplane.y)/4
			if(_airplane.x<(ValueObject.WIDTH/2+_DISTANCE)){
				arr.push({x:ValueObject.WIDTH/2+_DISTANCE,y:_airplane.y+y_distance});
				arr.push({x:ValueObject.WIDTH/2+2*_DISTANCE,y:_airplane.y+ 2*y_distance});
				arr.push({x:ValueObject.WIDTH/2+_DISTANCE,y:_airplane.y + 3*y_distance});
				arr.push({x:ValueObject.WIDTH/2,y:ValueObject.HEIGHT/2});
			}else{
				arr.push({x:_airplane.x +_DISTANCE,y:_airplane.y+y_distance});
				arr.push({x:_airplane.x+2*_DISTANCE,y:_airplane.y+ 2*y_distance});
				arr.push({x:_airplane.x+_DISTANCE,y:_airplane.y + 3*y_distance});
				arr.push({x:ValueObject.WIDTH/2,y:ValueObject.HEIGHT/2});
			}
			return arr;
		}
		private function airplaneDownComplete():void{
			_airplane.visible = false;
			Popup.getInsctance().show(this);
		}
		public function airplaneUp():void{
			_airplane.gotoAndPlay("closePU");
			//var arr:Array = generalOdinalUp();
			_airplane.visible = true;
			//TweenLite.to(_airplane,_DURATION,{bezierThrough:arr,orientToBezier:true,rotation:360,ease:Quart.easeInOut,onComplete:airplaneUpComplete});
		}
		private function generalOdinalUp():Array{
			var arr:Array=[];
			var y_distance:Number  = (ValueObject.HEIGHT/2-_Y_ODINAL)/4
			if(_no<3){
				arr.push({x:_no*ValueObject.WIDTH/4-_DISTANCE,y:ValueObject.HEIGHT/2-y_distance});
				arr.push({x:_no*ValueObject.WIDTH/4-2*_DISTANCE,y:ValueObject.HEIGHT/2- 2*y_distance});
				arr.push({x:_no*ValueObject.WIDTH/4-_DISTANCE,y:ValueObject.HEIGHT/2- 3*y_distance});
				arr.push({x:_no*ValueObject.WIDTH/4,y:_Y_ODINAL});
			}else{
				arr.push({x:ValueObject.WIDTH/2  -_DISTANCE,y:ValueObject.HEIGHT/2-y_distance});
				arr.push({x:ValueObject.WIDTH/2 - 2*_DISTANCE,y:ValueObject.HEIGHT/2- 2*y_distance});
				arr.push({x:ValueObject.WIDTH/2 - _DISTANCE,y:ValueObject.HEIGHT/2- 3*y_distance});
				arr.push({x:_no*ValueObject.WIDTH/4,y:_Y_ODINAL});
			}
			return arr;
		}
		private function airplaneUpComplete():void{
			_airplane.rotation = 0;
		}
			
	}
}